package com.eber.demo.design.decorator;

import lombok.extern.slf4j.Slf4j;

interface Component {
    void operation();
}

class ConcreteComponent implements Component {
    @Override
    public void operation() {
        System.out.println("ConcreteComponent operation.");
    }
}

class Decorator implements Component {
    private final Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    @Override
    public void operation() {
        // 在被装饰对象操作之前可以添加额外的行为
        preOperation();

        // 调用被装饰对象的方法
        component.operation();

        // 在被装饰对象操作之后可以添加额外的行为
        postOperation();
    }

    private void preOperation() {
        System.out.println("Pre operation action.");
    }

    private void postOperation() {
        System.out.println("Post operation action.");
    }
}

@Slf4j
public class DecorateApplication {
    public static void main(String[] args) {
        // 创建一个具体组件的实例
        Component component = new ConcreteComponent();

        // 创建装饰器实例，并将组件作为参数传入
        Component decoratedComponent = new Decorator(component);

        // 调用装饰器的operation方法，这将会执行装饰器中定义的逻辑
        decoratedComponent.operation();
    }
}
